package com.itsxb.configure;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class SatokenIntercepter implements RequestInterceptor {
    @Value("${sa-token.token-name}")
    private String tokenName;

    /**
     * 
     * @param requestTemplate 他是下一个Request请求
     */
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //使用RequestContextHolderServletRequestAttributes
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //然后在获取Request
        HttpServletRequest request = sra.getRequest();
        //通过Request根据tokenName获取Header 上一个Header
        String satokenValue = request.getHeader(tokenName);

        requestTemplate.header(tokenName,satokenValue);
    }
}
